﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Photon.SocketServer;
using WorldOfSuperpowers.Cache;
using WorldOfSuperpowers_COMMON;
using WorldOfSuperpowers_COMMON.DataModel;

namespace WorldOfSuperpowers.Handler
{
    public class LoginHandler:BaseHandler
    {
        public override void InitCode()
        {
            code = OperationCode.Login;
        }

        public override void TranslateDic(Dictionary<byte, object> message,MasterClientPeer peer, SendParameters sendParameters)
        {
            //todo 此处登陆需要检测的目前只有字符串
            object UserData = GetValueFromDic(code, message);
            if (UserData != null)
            {
                OperationResponse op = new OperationResponse();
                op.OperationCode = (byte)code;
                //登陆成功时检测是否需要重命名
                LoginModel Lmodel = ProtoTranslater.GetInstance().GetTypeFromBytes<LoginModel>(UserData);
                bool isNeedRename =  UserCache.LoginAction(Lmodel, peer, ref op);
                peer.SendOperationResponse(op, sendParameters);
                MasterServer.log.Debug("是否需要重命名:" + isNeedRename);
                //如果需要重命名，发送重命名消息
                if (isNeedRename)
                {
                    RenameAction(peer, sendParameters);
                }
            }
        }

        public static void RenameAction(MasterClientPeer peer,SendParameters sendParameters)
        {
            EventData data = new EventData();
            data.Code = (byte)EventCode.Rename;
            MasterServer.log.Debug("发送重命名event");
            //目前暂时不需要内容
            peer.SendEvent(data, new SendParameters());
        }
    }
}
